const noticeService = require('../services/noticeService');
const { success, successWithData, error } = require('../utils/resultwrapper');

class NoticeController {
    /**
     * 创建通知公告
     */
    async createNotice(req, res) {
        try {
            const noticeData = req.body;
            const result = await noticeService.createNotice(noticeData);
            res.json(successWithData(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 更新通知公告
     */
    async updateNotice(req, res) {
        try {
            const { id } = req.params;
            const noticeData = req.body;
            const result = await noticeService.updateNotice(id, noticeData);
            res.json(successWithData(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 删除通知公告
     */
    async deleteNotice(req, res) {
        try {
            const { id } = req.params;
            const result = await noticeService.deleteNotice(id);
            res.json(success(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 获取通知公告详情
     */
    async getNoticeById(req, res) {
        try {
            const { id } = req.params;
            const result = await noticeService.getNoticeById(id);
            res.json(success(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 获取通知公告列表
     */
    async getNoticeList(req, res) {
        try {
            const result = await noticeService.getNoticeList(req.query);
            res.json(success(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 发布通知公告
     */
    async publishNotice(req, res) {
        try {
            const { id } = req.params;
            const result = await noticeService.publishNotice(id);
            res.json(success(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 撤回通知公告
     */
    async revokeNotice(req, res) {
        try {
            const { id } = req.params;
            const result = await noticeService.revokeNotice(id);
            res.json(success(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }
}

module.exports = new NoticeController();